package com.shenglaotuan.community.article.controller;


import com.shenglaotuan.community.article.entity.Article;
import com.shenglaotuan.community.article.entity.ArticleSimpleVO;
import com.shenglaotuan.community.article.entity.ArticleVO;
import com.shenglaotuan.community.article.service.ArticleService;
import com.shenglaotuan.community.common.entity.JwtConfig;
import com.shenglaotuan.community.common.entity.ResponseResult;
import com.shenglaotuan.community.common.service.UploadService;
import com.shenglaotuan.community.common.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ruidong
 * @since 2020-08-22
 */
@Api(tags = "文章")
@Validated
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private JwtConfig jwtConfig;

    @Value("${dir-path.article}")
    private String articlePath;

    @ApiOperation("保存文章")
    @PostMapping("/addOrChange")
    public ResponseResult<Void> saveArticle(HttpServletRequest request, @Validated Article article, String[] tags){
        String uid = TokenUtils.getIdByRequest(request, jwtConfig);
        articleService.saveArticle(uid, article, tags, request.getServerName());
        return ResponseResult.responseSuccess();
    }

    @ApiOperation("查询文章总数")
    @GetMapping("/countTotal")
    public ResponseResult<Integer> countPage(String uid, String query){
        Integer total = articleService.countTotal(uid, query);
        return ResponseResult.responseSuccess(total);
    }

    @ApiOperation("查询文章大概")
    @GetMapping("/listSimple")
    public ResponseResult<List<ArticleSimpleVO>> listSimple(String uid, Integer page, Integer record, String query){
        List<ArticleSimpleVO> data = articleService.listSimple(uid, page, record, query);
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("获取某个文章")
    @GetMapping("/get")
    public ResponseResult<ArticleVO> get(String id, String host){
        ArticleVO data = articleService.getArticle(id, host);
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("删除某个文章")
    @PostMapping("/delete")
    public ResponseResult<Void> delete(String id){
        boolean data = articleService.removeById(id);
        return ResponseResult.responseSuccess();
    }

    @ApiOperation("上传头像")
    @PostMapping("/img/upload")
    public ResponseResult<String> uploadArticleImg(HttpServletRequest request, MultipartFile img, String dirNum){
        String id = TokenUtils.getIdByRequest(request, jwtConfig);
        String path = uploadService.uploadArticleImg(request, img, id, this.articlePath, dirNum);
        return ResponseResult.responseSuccess(path);
    }

    @ApiOperation("获取该用户下次上传的文件应该保存的文件夹序号")
    @GetMapping("/getUploadDirNum")
    public ResponseResult<Integer> getUploadDirNum(HttpServletRequest request){
        String id = TokenUtils.getIdByRequest(request, jwtConfig);
        int num = uploadService.getArticleUploadDirNum(request, id, this.articlePath);
        return ResponseResult.responseSuccess(num);
    }

    @ApiOperation("下载文章")
    @PostMapping("/download")
    public void download(HttpServletResponse response, String id, String host){
        articleService.download(response, id, host);
    }
}

